package com.wwwarehouse.contract.program_operation.select_industry;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.GridView;

import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.contract.adapter.SelectIndustryAdapter;
import com.wwwarehouse.contract.bean.ChooseModuleBean;
import com.wwwarehouse.contract.bean.SelectIndustryBean;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.event.IndusEvent;

import java.util.ArrayList;
import java.util.List;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * Creator by zhenwang.xiang on 2017/9/23 11:19
 * Description: 行业选择Fragment
 */


public class SelectIndustryFragment extends BaseFragment {

    private GridView mGridView;
    private List<SelectIndustryBean> mDataList;
    private SelectIndustryAdapter mSelectIndustryAdapter;
    List<ChooseModuleBean.ModuleList.BeanList> beanLists = new ArrayList<>();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_select_industry, container, false);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDataList = new ArrayList<>();
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        if (enter) {
            if (getArguments() != null && getArguments().getSerializable(ContractConstant.KEY_BUNDLE_VIEW_PAGER_DATA) != null) {
                final ChooseModuleBean moduleBean = (ChooseModuleBean) getArguments().getSerializable(ContractConstant.KEY_BUNDLE_VIEW_PAGER_DATA);
                int position = getArguments().getInt(ContractConstant.KEY_BUNDLE_POSITION);
                int pageNum = getArguments().getInt(ContractConstant.KEY_BUNDLE_PAGENUM);
                beanLists.clear();
                for (int i = position * pageNum; i < (position + 1) * pageNum; i++) {
                    if (i >= 0 && i < moduleBean.getModuleList().getList().size()) {
                        beanLists.add(moduleBean.getModuleList().getList().get(i));
                    }
                }
                mSelectIndustryAdapter = new SelectIndustryAdapter(beanLists, getActivity());
                mGridView.setAdapter(mSelectIndustryAdapter);
                mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        ChooseModuleBean.ModuleList.BeanList list = beanLists.get(position);
                        if (list.getSelected() == 1 && getArguments().getBoolean(ContractConstant.KEY_IS_FUNCTION)) {
                            return;
                        }
                        if (list.isSelect()) {
                            list.setSelect(false);
                        } else {
                            list.setSelect(true);
                        }
                        mSelectIndustryAdapter.notifyDataSetChanged();
                        LogUtils.showLog("点击后的 -----------------" + list.isSelect());
                        EventBus.getDefault().post(new IndusEvent(list));
                    }
                });

            }
        }
        return null;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mGridView = $(R.id.gridview);



    }

    @Override
    public void setTitle() {

    }

    @Override
    public void requestDatas() {
    }
}
